/* eslint-disable import/no-unresolved */
/* eslint-disable import/extensions */
/* eslint-disable import/no-extraneous-dependencies */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { observer } from 'mobx-react';
import { ToolTip, Message, Modal } from '@liepin/react-violet-pc';
import { Ajax, cookieUtil } from '@liepin/native-ltcore4c-pc';
import localCookie from '@liepin/native-sweet-fe';
// import AppDownload from '../../app-download/AppDownload';
// import ResumeToTop from './ResumeToTop';
// import RefreshResumeResult from './RefreshResumeResult';
// import ImproveDuty from './ImproveDuty';
// import improveDutyStore from '../store/improve-duty-store';

const needShowVasDialog = () => {
  const cookieUserId = cookieUtil.get('UniqueKey');
  return !localCookie.get(`fe_c_consultanshow_${cookieUserId}`);
};


export default function refreshResume(Button) {
  class RefreshResume extends Component {

    state = {
      showToTop: false,
      showTooltip: false,
      showSuccess: false,
      resultProps: {
        successMsg: '',
        success: false,
        resId: this.props.resume.resIdEncode,
        needImproveDuty: false,
      },
    };

    needShowVasDialogFlag = false;

    handleHideTooltip = () => {
      this.setState({ showTooltip: false });
    }

    handleSuccess = () => {
      this.setState({ showSuccess: false });
    }

    handleRefreshResume = () => {
      const { resume } = this.props;
      this.needShowVasDialogFlag = needShowVasDialog();
      if (this.needShowVasDialogFlag) {
        this.setState({ showToTop: true });
      } else if (resume.draftResume) {
        this.setState({ showTooltip: true });
        return;
      }
      this.refreshResume();
    }

    refreshResume() {
      const { resume } = this.props;
      Ajax({
        url: '/resume/refreshresume.json',
        data: { res_id_encode: resume.resIdEncode },
      })
        .then(({ flag, data, msg }) => {
          if (flag === 1) {
            const { msg: successMsg, workExpForm, res_id_encode: resId } = data;
            if (!this.needShowVasDialogFlag) {
              const cookieUserId = cookieUtil.get('UniqueKey');
              const refreshResumeCookie = `fe_c_appcode_refreshResume_${cookieUserId}`;
              if (refreshResumeCookie) {
                this.setState({
                  showTooltip: true,
                  resultProps: {
                    resId,
                    successMsg,
                    success: true,
                    needImproveDuty: workExpForm.needImproveDuty,
                  },
                });
                // improveDutyStore.init(workExpForm);
              } else {
                this.setState({ showSuccess: true });
                localCookie.set(refreshResumeCookie, '1', 30);
              }
            }
          } else {
            msg && Message.error(msg);
          }
        }).catch(() => {})
    }

    render() {
      const { position, arrowStyle, userInfo, onImproveDuty, resume, ...other } = this.props;
      const { resultProps, showTooltip, showSuccess, showToTop } = this.state;
      return (
        <>
          <ToolTip
            arrowStyle={ arrowStyle }
            position={ position }
            uiStyle={ resultProps.success ? 'success' : 'warning' }
            visible={ showTooltip }
            title={ <span { ...resultProps }/> }
            onOutsideClick={ this.handleHideTooltip }
          ><Button onClick={ this.handleRefreshResume } { ...other }/>
          </ToolTip>
          { showToTop ? <span/> : null }
          {
            showSuccess
              ? (
                <Modal visible={ showSuccess } onClose={ this.handleSuccess }>
                  <span/>
                </Modal>
              ) : null
          }
          {/* {
            improveDutyStore.inited
              ? <ImproveDuty
                store={ improveDutyStore }
                userInfo={ userInfo }
                onSuccess={ onImproveDuty }
              />
              : null
          } */}
        </>
      );
    }
  }

  RefreshResume.propTypes = {
    arrowStyle: PropTypes.object,
    userInfo: PropTypes.object.isRequired,
    onImproveDuty: PropTypes.func,
    position: PropTypes.string,
    resume: PropTypes.object.isRequired,
  };
  
  RefreshResume.defaultProps = {
    position: 'left',
    onImproveDuty: () => {},
    arrowStyle: null,
  }

  return observer(RefreshResume);
}
